/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  TietoEnator
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "gpstestwidget.h"
#include "ui_gpstestwidget.h"

#include <termios.h>

#include "../util/serialport.h"
#include "../util/lineterminal.h"
#include "../gps/nmeaparser.h"

using namespace util;
using namespace gps;
using namespace client;

CGPSTestWidget::CGPSTestWidget(CSerialPort *pSerialPort,
    CLineTerminal *pLineTerminal, CNMEAParser *pNmeaParser, QWidget *pParent)
    :QWidget(pParent),
    mUi(new Ui::CGPSTestWidget),
    mSerialPort(pSerialPort),
    mLineTerminal(pLineTerminal),
    mNmeaParser(pNmeaParser)
{
    mUi->setupUi(this);

    //debug output
    QObject::connect(mLineTerminal, SIGNAL(lineReceived(QString)),
                     this, SLOT(onLineReceived(QString)));

    QObject::connect(
        mNmeaParser, SIGNAL(GPGGAreceived(QTime,gps::CGPSPosition,gps::CGPSFix,int,float,float,float,float,int)),
        this, SLOT(onGPGGAreceived(QTime,gps::CGPSPosition,gps::CGPSFix,int,float,float,float,float,int)));
    QObject::connect(
        mNmeaParser, SIGNAL(GPVTGreceived(float,float,float,float)),
        this, SLOT(onGPVTGreceived(float,float,float,float)));

}

CGPSTestWidget::~CGPSTestWidget()
{
    delete mUi;
}

void CGPSTestWidget::on_pushButtonOpen_clicked()
{
    if (mSerialPort->open("/dev/rfcomm1", B4800))
        mUi->plainTextEdit->appendPlainText("Port Opened\n");
    else
        mUi->plainTextEdit->appendPlainText("Port Opening Failed\n");
}

void CGPSTestWidget::on_pushButtonClose_clicked()
{
    if (mSerialPort->close())
        mUi->plainTextEdit->appendPlainText("Port Closed\n");
    else
        mUi->plainTextEdit->appendPlainText("Port Closing Failed\n");
}

void CGPSTestWidget::onLineReceived(const QString &pLine)
{
    mUi->plainTextEdit->appendPlainText(pLine);
}

void CGPSTestWidget::onGPGGAreceived(const QTime &pTime,
        const CGPSPosition &pPosition, const CGPSFix &pFix, int pSatelites,
        float pHdop, float pAltitude, float pGeoidHeight,
        float pDgpsUpdateDelay, int pDgpsStationId)
{
    mUi->leTime->setText(pTime.toString());
    mUi->leLatitude->setText(pPosition.latitude().toString());
    mUi->leLongitude->setText(pPosition.longitude().toString());
    mUi->leFix->setText(pFix.toString());
    mUi->leSatelites->setText(QString::number(pSatelites));
    mUi->leHDOP->setText(QString::number(pHdop));
    mUi->leAltitude->setText(QString::number(pAltitude));
    mUi->leUpdateDelay->setText(QString::number(pDgpsUpdateDelay));
    mUi->leGeoidHeight->setText(QString::number(pGeoidHeight));
    mUi->leUpdateRefId->setText(QString::number(pDgpsStationId));
}

void CGPSTestWidget::onGPVTGreceived(float pHeadingTrueNorth,
        float pHeadingMagnetic, float pSpeedInKnots, float pSpeedInKilometers)
{
    mUi->leHeadingTN->setText(QString::number(pHeadingTrueNorth));
    mUi->leHeadingMN->setText(QString::number(pHeadingMagnetic));
    mUi->leSpeedKmH->setText(QString::number(pSpeedInKilometers));
    mUi->leSpeedKnH->setText(QString::number(pSpeedInKnots));
}
